package com.zzw.controller;

import com.zzw.entity.PageResult;
import com.zzw.entity.Result;
import com.zzw.message.MessageConstant;
import com.zzw.pojo.Consumer;
import com.zzw.service.ConsumerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @GetMapping("/{pageNum}/{pageSize}")
    public PageResult findPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageInfo pageInfo = consumerService.findPage(pageNum, pageSize);
        long total = pageInfo.getTotal();
        List rows = pageInfo.getList();
        return new PageResult(total,rows);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Consumer> list = consumerService.findAll();
            return new Result(true,MessageConstant.QUERY_CONSUMER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CONSUMER_FAIL);
        }
    }


    @PostMapping("/add")
    public Result addConsumer(@RequestBody Consumer consumer){
        try {
            consumerService.addConsumer(consumer);
            Result result = new Result(true, MessageConstant.ADD_CONSUMER_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result result = new Result(false, MessageConstant.ADD_CONSUMER_FAIL);
        return result;
    }
}
